/**
 * @Author: oldTea
 * @CreateTime: 2024-12-05
 * @Description:
 * @Version: 1.0
 */
package com.ambition.demo.developUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 *@Author: oldTea
 *@CreateTime: 2024-12-05
 *@Description:
 *@Version: 1.0
 */
public class SqlUtils {


    public static void main(String[] args) throws IOException {

        //生成dm
        createDmSql();
        //生成查询sql
        //createSelectSql();
        System.out.println("生成完毕！");

    }

    private static void createSelectSql() throws IOException {
        BufferedReader bufferedReader = null;
        BufferedWriter writer = null;
        try{
            bufferedReader = new BufferedReader(new FileReader("F:\\desk\\aa.txt"));
            writer = new BufferedWriter(new FileWriter("F:\\desk\\bb.txt",false));
            List<String> lines = new ArrayList<>();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }

            for (int i = 0; i < lines.size(); i++) {

                if(lines.get(i).equals("deleted")){
                    writer.write("case when deleted = 'f' then '0'\n" +
                            "else '1'\n" +
                            "end as deleted");
                }else {
                    writer.write(lines.get(i));
                }
                if (i < lines.size() - 1) { // 如果不是最后一行
                    writer.write(",");
                    writer.newLine();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bufferedReader.close();
            writer.close();
        }
    }

    private static void createDmSql() throws IOException {
        BufferedReader bufferedReader = null;
        BufferedWriter writer = null;
        try{
            bufferedReader = new BufferedReader(new FileReader("F:\\desk\\aa.txt"));
            writer = new BufferedWriter(new FileWriter("F:\\desk\\bb.txt",false));

            writer.write("when not matched then insert values ( ");
            List<String> lines = new ArrayList<>();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                lines.add("source." + "\"" + line + "\"");
            }

            for (int i = 0; i < lines.size(); i++) {
                writer.write(lines.get(i));
                if (i < lines.size() - 1) { // 如果不是最后一行
                    writer.write(",");
                    writer.newLine();
                }
            }
            writer.write(");");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            bufferedReader.close();
            writer.close();
        }
    }
}


